package com.shike.hq.controller;


import com.shike.hq.entity.Admin;
import com.shike.hq.security.AdminUserDetailsService;
import com.shike.hq.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;

/**
 * The type Login controller.
 */
@Controller
@RequestMapping("/loginPage")
public class LoginController {

  @Autowired
  private AdminService adminService;
//  @Autowired
//  private AdminUserDetailsService customUserDetailsService;

  /**
   * 登录页面映射
   *
   * @return login页面 string
   */
  @GetMapping
  public String loginPage(){
    System.out.println("进入登录页面");
    return "login";
  }

  /**
   * 登录方法.
   *
   * @param username   输入的用户名
   * @param password   输入的密码
   * @param session    session对象
   * @param attributes attributes对象
   * @return 登录页面映射
   */
  // 取消自定义登录
//  @PostMapping("/login")
//  public String login(@RequestParam("username") String username,
//                      @RequestParam("password") String password,
//                      HttpSession session, RedirectAttributes attributes) {
//    System.out.println("login执行登录验证过程");
//    Admin admin = adminService.selectAdminByUsernameAndPassword(username, password);
//    if (admin != null) {
//      //登录成功
//      admin.setLastLogin(new Timestamp(new Date().getTime()));
//      adminService.updateById(admin);
//      session.setAttribute("admin", admin);
//      System.out.println("登录成功");
//      //防止表单重复提交，重定向
//      return "redirect:/index";
//    } else {
//      //登陆失败
//      attributes.addFlashAttribute("msg", "用户名或密码错误");
//      System.out.println("登录失败，返回loginPage");
//      return "redirect:/loginPage";
//    }
//  }


  /**
   * 登出方法。
   *
   * @param session session对象
   * @return 重定向到登录页 string
   */
  @GetMapping("/logout")
  public String logout(HttpSession session) {
    session.invalidate();
    return "redirect:/loginPage";
  }


}
